package com.cl.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.cl.bean.Client;
import com.cl.service.ClientService;
import com.cl.utils.PageBean;
import com.cl.web.base.BaseAction;

import net.sf.json.JSONObject;

@Controller
@Scope("prototype")
public class ClientAction extends BaseAction<Client> {
	
	@Autowired
	private ClientService clientService;
	
	private int e_id;
	
	public void setE_id(int e_id) {
		this.e_id = e_id;
	}

	public String add(){
		return LIST;
	}
	
	public String findByPage() throws IOException{
		PageBean pb = clientService.getPageBean(dc,page,limit);
		ObjectToJson(pb,new String[]{"cutterPage","pageSize","start","emp"});
		return NONE;
	}
	
	public String findByEid() throws IOException{
		List<Client> list = clientService.findByEid(e_id);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/json;charset=utf-8");
		PrintWriter pw = response.getWriter();
		JSONObject json = new JSONObject();
		if(list.size() > 0){
			json.put("result", "no ok");
			pw.println(json.toString());
		}else{
			json.put("result", "ok");
			pw.println(json.toString());
		}
		return NONE;
	}
}
